<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>

    <!-- 登录表单 -->
    <van-form ref="loginForm" @submit="onSubmit">
      <van-cell-group>
        <van-field v-model="user.mobile" name="mobile" required clearable placeholder="请输入手机号"
          :rules="userFormRules.mobile" maxlength=11>
          <i slot="left-icon" class="toutiao toutiao-shouji"></i>
        </van-field>
        <van-field v-model="user.code" name="code" required type="number" placeholder="请输入验证码"
          :rules="userFormRules.code" maxlength=6>
          <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
          <!-- 倒计时 -->
          <van-count-down slot="button" :time="1000*60" format="ss s" v-if="isVanCountDown"
            @finish="isVanCountDown=false" />
          <van-button v-else slot="button" class="send-sms-btn" round size="small" native-type="button"
            @click="onSendSms">
            发送验证码
          </van-button>
        </van-field>
      </van-cell-group>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
// 引入登录请求
import { login, getSendSms } from '@/api/user.js'
export default {
  name: 'LoginPage',
  data () {
    return {
      user: {
        mobile: '', // 手机号
        code: '' // 验证码
      },
      // 登录校验规则
      userFormRules: {
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^1[3|5|7|8]\d{9}$/, message: '手机格式错误' }
        ],
        code: [
          { required: true, message: '验证码不能为空' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]
      },
      // 控制倒计时显示隐藏
      isVanCountDown: false
    }
  },
  methods: {
    //  验证码发送验证
    async onSendSms () {
      // console.log('123')
      // 手机号校验
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return console.dir('验证失败', err)
      }

      // 发送验证码
      try {
        await getSendSms(this.user.mobile)
        this.$toast.success('发送成功')
        // 显示倒计时
        this.isVanCountDown = true
        // console.log(res)
      } catch (err) {
        this.isVanCountDown = false
        if (err.response && err.response.status === 429) {
          this.$toast.fail('发送频繁，请稍候再试')
        } else {
          this.$toast.fail('发送失败请稍等再试')
        }
        console.dir(err)
      }
    },
    // 提交登录验证
    async onSubmit () {
      // 加载提示
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true, // 禁止点击背景
        duration: 0 // 持续展示 toast
      })

      try {
        const res = await login(this.user)
        // console.log(res, '成功')
        // res.data.data=>{token:'',refresh_token:''}
        this.$store.commit('setUser', res.data.data)
        this.$toast.success('登录成功')
        this.$router.push('/my')
      } catch (err) {
        // console.log(err)
        if (err.response && err.response.status === 400) {
          // console.log(err, '失败')
          this.$toast.fail('手机号或验证码错误')
        } else {
          this.$toast.fail('登录失败，请稍候再试')
        }
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
.login-container {
  .page-nav-bar {
    .van-icon {
      color: #fff;
    }
  }
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 156px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .van-button--small {
    padding: 0;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border-radius: 10px;
      border: none;
    }
  }
}
</style>
